#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
	PROJECT pyevocomm -
 
	APP		Core 

	MODULE	 gest view

	SITE DESCRIPTION http://code.google.com/p/pyevocomm/ 

	@copyright: 2012 by archetipo <alessio.gerace@gmail.com>
	@license: New BSD License, see COPYING for details.
"""

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../..'))

# stdlib


#pyglet
from pyglet.gl import *
from pyglet import gl, font
# cocos2d related
from cocos.layer import Layer, ColorLayer
from cocos.scene import Scene
from cocos.director import director
from cocos.actions import *

# evocomm related
from evoctrl import *
import importlib
import soundex
import Tlib.Dprime.TTSClient as tts



__all__ = ['get_newgame']

class BackgroundLayer( Layer ):
	def __init__(self):
		vw,vh=director.get_window_size()
		super( BackgroundLayer, self ).__init__()
		self.img = pyglet.resource.image('paper.jpg')
		self.img.width=vw
		self.img.height=vh
		
	def draw( self ):
		glPushMatrix()
		self.transform()
		self.img.blit(0,0)
		glPopMatrix()

class MainView( Layer ):

	def __init__(self, model):
		super(MainView,self).__init__()

		self.model = model

		self.model.push_handlers( self.on_select, \
								  self.on_close, \
								  self.on_step, \
								  self.on_inselect, \
								  self.on_lost, \
								  self.on_right, \
								  self.on_left, \
								  self.on_single,\
								  self.on_lang,\
								  self.on_menu,\
								  self.on_reload,\
								  
									)
		soundex.play('init.mp3')
		#self.hud.show_message( 'GET READY', self.model.start )

	def on_select(self):
		soundex.play('select.mp3')
		return True

	def on_close(self):
		super(MainView,self).on_exit()
		soundex.stop_music()

	def on_step( self):
		#~ soundex.play('move.mp3')
		#~ print model.currspeech
		tts.parla(self.model.currspeech)
		return True

	def on_inselect(self ):
		#~ soundex.play('move.mp3')
		return True

	def on_lost(self ):

		return True

	def on_right( self ):

		return True

	def on_left( self ):

		return True

	def on_single( self ):
		return True

	def on_lang( self ):
	   
		return True
		
	def on_menu( self ):
		tts.parla(self.model.currspeech)
		return True
	
	def on_reload( self ):
		tts.parla(self.model.currspeech)
		return True

	def draw( self ):
		glPushMatrix()
		self.transform()
		self.model.draw()
		glPopMatrix()

def get_newgame(modell,controller):
	'''returns the game scene'''
	scene = Scene()

	# view
	view = MainView( modell)

	# set controller in model
	modell.set_controller( controller )

	# add controller
	scene.add( controller, z=1, name="controller" )
	scene.add( BackgroundLayer(), z=0, name="background" )
	scene.add( view, z=2, name="view" )

	return scene


